package inet.app.view.fragment;

import inet.app.R;
import inet.app.model.object.HeaderInfo;
import inet.app.model.object.Province;
import inet.app.view.activity.LichMoThuongDetail;
import inet.app.view.activity.MyApp;
import inet.app.view.adapter.LichMoThuongAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

public class FragmentLmtMN extends BaseFragmentMT implements OnClickListener,
		OnTouchListener, OnChildClickListener {

	private LinkedHashMap<String, HeaderInfo> myDepartments = new LinkedHashMap<String, HeaderInfo>();
	private ArrayList<HeaderInfo> deptList = new ArrayList<HeaderInfo>();
	private LichMoThuongAdapter listAdapter;
	private ExpandableListView myList;

	public static Fragment newInstance(Context context) {
		FragmentLmtMN f = new FragmentLmtMN();
		return f;
	}

	public static Fragment newInstance(Context context, String position) {
		FragmentLmtMN f = new FragmentLmtMN();
		Bundle args = new Bundle();
		args.putString("pos", position);
		f.setArguments(args);
		return f;
	}

	public static Fragment newInstance(Context context, String position,
			String json) {
		FragmentLmtMN f = new FragmentLmtMN();
		Bundle args = new Bundle();
		args.putString("pos", position);
		args.putString("json", json);
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(
				R.layout.fragment_mothuong, null);
		loadDataMN();
		// get reference to the ExpandableListView
		myList = (ExpandableListView) root
				.findViewById(R.id.exListViewLichMoThuong);
		// create the adapter by passing your ArrayList data
		listAdapter = new LichMoThuongAdapter(getActivity(), deptList);
		// attach the adapter to the list
		myList.setAdapter(listAdapter);

		// expand all Groups
		expandAll();

		// listener for child row click
		myList.setOnChildClickListener(this);
		// listener for group heading click
		myList.setOnGroupClickListener(myListGroupClicked);
		return root;
	}

	// method to expand all groups
	private void expandAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			myList.expandGroup(i);
		}
	}

	// method to collapse all groups
	private void collapseAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			myList.collapseGroup(i);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	private void loadDataMN() {

		addProduct("Thứ 2", "XSHCM", "Hồ Chí Minh");
		addProduct("Thứ 2", "XSDT", "Đồng Tháp");
		addProduct("Thứ 2", "XSCM", "Cà Mau");

		addProduct("Thứ 3", "XSBTR", "Bến Tre");
		addProduct("Thứ 3", "XSVT", "Vũng Tàu");
		addProduct("Thứ 3", "XSBL", "Bạc Liêu");

		addProduct("Thứ 4", "XSDN", "Đồng Nai");
		addProduct("Thứ 4", "XSST", "Sóc Trăng");
		addProduct("Thứ 4", "XSCT", "Cần Thơ");

		addProduct("Thứ 5", "XSAG", "An Giang");
		addProduct("Thứ 5", "XSTN", "Tây Ninh");
		addProduct("Thứ 5", "XSBTH", "Bình Thuận");

		addProduct("Thứ 6", "XSBD", "Bình Dương");
		addProduct("Thứ 6", "XSVL", "Vĩnh Long");
		addProduct("Thứ 6", "XSTV", "Trà Vinh");

		addProduct("Thứ 7", "XSHCM", "Hồ Chí Minh");
		addProduct("Thứ 7", "XSLA", "Long An");
		addProduct("Thứ 7", "XSBP", "Bình Phước");
		addProduct("Thứ 7", "XSHG", "Hậu Giang");

		addProduct("Chủ Nhật", "XSTG", "Tiền Giang");
		addProduct("Chủ Nhật", "XSKG", "Kiên Giang");
		addProduct("Chủ Nhật", "XSDL", "Đà Lạt");
	}

	// our group listener
	private OnGroupClickListener myListGroupClicked = new OnGroupClickListener() {

		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {

			// get the group header
			HeaderInfo headerInfo = deptList.get(groupPosition);
			// display it or do something with it

			return false;
		}

	};

	// here we maintain our products in various departments
	private int addProduct(String department, String code, String title) {

		int groupPosition = 0;

		// check the hash map if the group already exists
		HeaderInfo headerInfo = myDepartments.get(department);
		// add the group if doesn't exists
		if (headerInfo == null) {
			headerInfo = new HeaderInfo();
			headerInfo.setName(department);
			myDepartments.put(department, headerInfo);
			deptList.add(headerInfo);
		}

		// get the children for the group
		ArrayList<Province> productList = headerInfo.getProductList();
		// size of the children list
		int listSize = productList.size();
		// add to the counter
		listSize++;

		// create a new child and add that to the group
		Province detailInfo = new Province();
		detailInfo.setCode(code);
		detailInfo.setTitle(title);
		productList.add(detailInfo);
		headerInfo.setProductList(productList);

		// find the group position inside the list
		groupPosition = deptList.indexOf(headerInfo);
		return groupPosition;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		// get the group header
		HeaderInfo headerInfo = deptList.get(groupPosition);
		// get the child info
		Province detailInfo = headerInfo.getProductList().get(childPosition);
		Intent intent = new Intent(getActivity(), LichMoThuongDetail.class);
		Bundle rb = new Bundle();
		rb.putString("code", detailInfo.getCode());
		rb.putString("name", detailInfo.getTitle());
		rb.putString("REGION", "MT");
		intent.putExtras(rb);
		startActivity(intent);
		return false;
	}

	public void onResume() {
		// TODO Auto-generated method stub
		if (((MyApp) getActivity().getApplication()).isKill()) {
			getActivity().finish();
		} else {
			((MyApp) getActivity().getApplication()).setRunning(true);
		}
		super.onResume();
	}
}
